﻿using System;

namespace Serializer
{
    public class SettingsManager
    {
        static string Extension = ".xml";
        static string Directory = "Content\\Settings\\";

        /// <summary>
        /// Loaded settings - Not currently implemented
        /// </summary>
        //SerializableDictionary<string, SettingsBase> _settings;

        /// <summary>
        /// Load a settings file
        /// </summary>
        public static T Load<T>()
        {
            Type t = typeof(T);
            return Load<T>(t.Name);
        }

        /// <summary>
        /// Attempt to load a settings file.  If not found, it will
        /// load a new, default instance of the settings class.
        /// </summary>
        public static T Load<T>(string name)
        {
            try
            {
                return Serializer.Read<T>( SettingsManager.Directory + name + SettingsManager.Extension);
            }
            catch (Exception e)
            {
                e = null;
                Type t = typeof(T);
                return (T)Activator.CreateInstance(t);
            }
        }

        /// <summary>
        /// Save a settings object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void Save<T>(T o)
        {
            Type t = typeof(T);
            Save<T>(o, t.Name);
        }

        /// <summary>
        /// Save a settings object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="file_name"></param>
        public static void Save<T>(T o, string name)
        {
            Serializer.Write<T>( o, SettingsManager.Directory + name + SettingsManager.Extension);
        }
    }
}
